import axios from "axios"
import qs from 'qs'
import BaseConfig from "../utils/baseConfig.js"
import { getLib,getBooks } from '../api/lib'
interface formBookItems{
    "classid": string,
    "mediaId": number,
    "title":string,
    "mediaPic":string,
    "description":string,
    "publisher":string,
    "author":string,
    "lowestPrice": string  
}
enum openType{
    formtype=1,
    importype=2
}
/*添加新书*/
class Addbook {
    element: HTMLElement
    pagewin: HTMLElement
    importbook:HTMLElement
    winstatus: boolean = false//弹窗按纽状态默认关闭
    formItems: any
    formBookItems:any
    winSelect:number=1
    constructor() {
        this.element = document.getElementById("addBook")!;
        this.pagewin = document.getElementById('page_win')!;
        this.importbook=document.getElementById('importbook')!;
        this.init();
    }
    init() {        
        this.element.addEventListener('click', this.openWin.bind(this));
        (document.getElementById('importBtn') as HTMLElement).addEventListener('click', this.importWin.bind(this));        
    }
    openWin() {//添加新书打开弹窗
        this.winstatus=false;//弹窗按纽状态关闭
        this.pagewin.style.display = 'block';
        this.winSelect=1;
        this.selectPage();
    }
    importWin(){//直接导入新书弹窗
        this.winstatus=false;//弹窗按纽状态关闭
        this.pagewin.style.display = 'block';
        this.winSelect=2
        this.selectPage();
    }
    selectPage(){//选择盒子
        let _this=this;
        (document.getElementById('close_page_win') as HTMLElement).addEventListener('click', this.checkStatus.bind(this));
        if(_this.winSelect==openType.formtype){//添加盒子
            (document.getElementById('addbooks') as HTMLElement).style.display = 'block';//显示添加盒子
            this.importbook.style.display='none';//隐藏导入盒子            
            (document.getElementById('submit_bgn') as HTMLElement).addEventListener('click', this.change.bind(this));//添加新书
        }else if(_this.winSelect==openType.importype){//导入盒子
            (document.getElementById('addbooks') as HTMLElement).style.display='none';//隐藏添加盒子
            this.importbook.style.display='block';//显示导入盒子
            (document.getElementById('submit_bgn3') as HTMLElement).addEventListener('click', this.handleImportBook.bind(this));//导入新书
        }
    }
    checkStatus() {//关闭弹窗
        this.winstatus = !this.winstatus
        if (this.winstatus) {//当弹窗按纽状态为true时，点击时隐藏所有表单盒子
            this.pagewin.style.display = 'none';
            (document.getElementById('addbooks') as HTMLElement).style.display = 'none';
            this.importbook.style.display='none';
        }
    }
    change() {//添加新书
        let _this = this;
        let mediaId=(document.getElementById('books_item_num') as HTMLInputElement).value
        _this.formItems = {
            "classid": (document.getElementById('books_item_type') as HTMLInputElement).value,
            "title": (document.getElementById('books_item_title') as HTMLInputElement).value,
            "mediaId": parseInt(mediaId),
            "mediaPic": (document.getElementById('books_item_img') as HTMLInputElement).value,
            "description": (document.getElementById('books_item_dec') as HTMLInputElement).value,
            "publisher": (document.getElementById('books_item_publish') as HTMLInputElement).value,
            "author": (document.getElementById('books_item_auth') as HTMLInputElement).value,
            "lowestPrice": (document.getElementById('books_item_price') as HTMLInputElement).value,
        }
        //console.log('_this.formItems',_this.formItems)
        axios({
            method: 'post',
            url: BaseConfig.apiUrl + '/' + BaseConfig.apiList.book[1].urls,
            headers: {
                'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
                'x-apicloud-mcm-key': BaseConfig.apiKey
            },
            data: qs.stringify(_this.formItems)
        }).then(function (response) {
            console.log(response);
            _this.clearInput();
            setTimeout(function(){
                location.reload();//刷新
            },2000)
        }).catch(function (error) {
            console.log(error);
        });
    }
    handleImportBook(){//根据mediaId导入新书
        let _this = this;
        let mediaId=(document.getElementById('impbook_item_num') as HTMLInputElement).value;        
        _this.getCategory(mediaId).then((res)=>{//查询图书信息
            console.log('得到图书信息',res);
            /*先查询图书信息，将获得的信息分别赋值给新增图书对应的属性*/
            if(res.data.mediaVo){
                let formBookItems:formBookItems = {
                    "classid": (document.getElementById('impbook_item_type') as HTMLInputElement).value,
                    "mediaId": parseInt(mediaId),
                    "title":res.data.mediaVo.title,
                    "mediaPic":res.data.mediaVo.coverPic,
                    "description":res.data.mediaVo.descs,
                    "publisher":res.data.mediaVo.publisher,
                    "author":res.data.mediaVo.authorPenname,
                    "lowestPrice": res.data.mediaVo.salePrice
                }
                console.log('formBookItems',formBookItems);
                axios({
                    method: 'post',
                    url: BaseConfig.apiUrl + '/' + BaseConfig.apiList.book[1].urls,
                    headers: {
                        'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
                        'x-apicloud-mcm-key': BaseConfig.apiKey
                    },
                    data: qs.stringify(formBookItems)
                }).then(function (response) {
                    console.log('导入成功');                
                    console.log(response);
                    setTimeout(function(){
                        location.reload();//刷新
                    },2000)
                }).catch(function (error) {
                    console.log(error);
                });
            }            
        })
    }
    getCategory(epubID:string){//根据epubID查询图书信息    
        let _this=this
        let params={
            action: 'getPcMediaInfo',
            consumeType: 1,
            platform: 3,
            deviceType: 'Android',
            deviceVersion: '5.0.0',
            channelId: 70000,
            platformSource: 'DDDS-P',
            fromPaltform: 'ds_android',
            deviceSerialNo: 'html5',
            clientVersionNo: '5.8.4',
            epubID:epubID,
            token: '',
            wordSize: 2,
            style: 2
        }
        return new Promise<any>((resolve:(value:any)=>void,reject)=>{
            getLib(params).then((res:any) => {
                if(res.status==200){
                    console.log('信息解决成功');
                    console.log(res.data);
                    //解决时返回得到的信息
                    resolve(res.data);                    
                }
            })            
        })
        
    }
    clearInput() {//清空表单
        (document.getElementById('books_item_title') as HTMLInputElement).value = '';
        (document.getElementById('books_item_num') as HTMLInputElement).value = '';
        (document.getElementById('books_item_img') as HTMLInputElement).value = '';
        (document.getElementById('books_item_dec') as HTMLInputElement).value = '';
        (document.getElementById('books_item_publish') as HTMLInputElement).value = '';
        (document.getElementById('books_item_auth') as HTMLInputElement).value = '';
        (document.getElementById('books_item_price') as HTMLInputElement).value = '';
    }
}
export default Addbook